{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=notebooks/107_copernicus.ipynb)\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/notebooks/107_copernicus.ipynb)\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
    "\n",
    "**Visualizing Copernicus data interactively with Leafmap**\n",
    "\n",
    "To learn more about Copernicus data, please visit https://dataspace.copernicus.eu.\n",
    "\n",
    "Uncomment the following line to install [leafmap](https://leafmap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U leafmap localtileserver jupyter-server-proxy xarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import rasterio\n",
    "import leafmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "To use Copernicus data, you need to get your own credentials. Follow the instructions [here](https://documentation.dataspace.copernicus.eu/APIs/S3.html) to get the credentials. Then, replace the `os.environ[\"AWS_ACCESS_KEY_ID\"]` and `os.environ[\"AWS_SECRET_ACCESS_KEY\"]` with your own credentials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"AWS_ACCESS_KEY_ID\"] = \"YOUR_ACCESS_KEY_ID\"\n",
    "os.environ[\"AWS_SECRET_ACCESS_KEY\"] = \"YOUR_SECRET_ACCESS_KEY\"\n",
    "os.environ[\"AWS_S3_ENDPOINT\"] = \"eodata.dataspace.copernicus.eu\"\n",
    "os.environ[\"AWS_VIRTUAL_HOSTING\"] = \"FALSE\"\n",
    "os.environ[\"GDAL_HTTP_UNSAFESSL\"] = \"YES\"\n",
    "os.environ[\"AWS_HTTPS\"] = \"YES\"\n",
    "os.environ[\"GDAL_HTTP_TCP_KEEPALIVE\"] = \"YES\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "You can create a `~/.s3cfg` file to store the credentials. Then use the following command to list the data.\n",
    "\n",
    "```bash\n",
    "s3cmd -c .s3cfg ls s3://eodata/\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "Let's try out the sample dataset of global water bodies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"/vsis3/eodata/CLMS/bio-geophysical/water_bodies/wb_global_1km_10daily_v2/2018/01/01/c_gls_WB_201801010000_GLOBE_PROBAV_V2.1.1_cog/c_gls_WB-WB_201801010000_GLOBE_PROBAV_V2.1.1.tiff\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "with rasterio.open(url) as src:\n",
    "    print(src.profile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "Visualize the water bodies interactively with Leafmap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map()\n",
    "m.add_raster(url, vmin=1, vmax=100, colormap=\"Blues\", layer_name=\"Water Bodies\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "![](https://github.com/user-attachments/assets/80f69503-6f0b-495d-b4ca-6773b0714299)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
